// 基础数据-客户与产品-产品相容-zhangx
import React, { Component } from 'react'
import {fetchData} from '../../components/AjaxConsumer'
import {ListBox} from '../../components'
import ProductModalTable from './productModalTable'
import {
    Form,
    Input,
    Tooltip,
    Icon,
    Cascader,
    Select,
    Row,
    Col,
    Checkbox,
    Button,
    message,
    Modal,
    Radio
} from 'antd';
const RadioGroup = Radio.Group;
const FormItem = Form.Item;
const Option = Select.Option;
const confirm = Modal.confirm;

class CompatibleManage extends Component{
    constructor(props) {
        super(props);
        this.state = {
            visible:false,
            confirmDirty: false,
            loading: false,
            addFlag: false,//false则为编辑
            groupTypeList:[
                {selectValue:'1',SelectName:'相斥'},
                {selectValue:'2',SelectName:'相似'}
            ],
            forceSeparateList:[
                {selectValue:'1',SelectName:'是'},
                {selectValue:'0',SelectName:'否'}
            ],
            activeFlagList:[
                {selectValue:'1',SelectName:'是'},
                {selectValue:'0',SelectName:'否'}
            ],
            formData:{
                productCompatibleId:'',
                groupCode:'',
                groupName:'',
                productCode:'',
                productName:'',
                groupType:'',
                forceSeparate:''
            }

        };
        //行操作列表-已弃用
        this.rowOperation = [
            
        ];
        //header 操作列表
        this.headerOperation = [
            {
                handleName: 'add',
                showName: '新增',
                iconClass: 'plus'
            },{
                handleName: 'edit',
                showName: '修改',
                iconClass: 'edit'
            },{
                handleName: 'delete',
                showName: '删除',
                iconClass: 'delete'
            }
        ];

        //table列表展示列配置
        //showName、columnName 必有属性， columnWidth 可选，默认100, columnHidden: true 列隐藏
        // foreverHidden永久隐藏
        this.tableColumn = {
            storageKey: 'ListProductCompatibleColumnConfig',
            data: [
                {
                    showName: 'id',
                    columnName: 'productCompatibleId',
                    columnHidden:true,
                    foreverHidden: true
                },
                {
                    showName: '组编码',
                    columnName: 'groupCode'
                },{
                    showName: '组名称',
                    columnName: 'groupName'
                },{
                    showName: '产品编码',
                    columnName: 'productCode'
                },{
                    showName: '产品名称',
                    columnName: 'productName'
                },{
                    showName: '组类型',
                    columnName: 'groupType',
                    showOption:[
                        {
                            optionKey:'',
                            optionShow:''
                        },{
                            optionKey:'1',
                            optionShow:'相斥'
                        },{
                            optionKey:'2',
                            optionShow:'相似'
                        }
                    ]
                },{
                    showName: '强制分离',
                    columnName: 'forceSeparate',
                    showOption:[
                        {
                            optionKey:'',
                            optionShow:''
                        },{
                            optionKey:'1',
                            optionShow:'是'
                        },{
                            optionKey:'0',
                            optionShow:'否'
                        }
                    ]
                },{
                    showName: '激活标记',
                    columnName: 'activeFlag',
                    showOption:[
                        {
                            optionKey:'1',
                            optionShow:'启用'
                        },{
                            optionKey:'2',
                            optionShow:'禁用'
                        }
                    ]
                },{
                    showName: '创建人',
                    columnName: 'createBy',
                    columnHidden:true
                },{
                    showName: '创建时间',
                    columnName: 'gmtCreate',
                    columnHidden:true
                },{
                    showName: '更新人',
                    columnName: 'lastModifiedBy',
                    columnHidden:true
                },{
                    showName: '更新时间',
                    columnName: 'gmtModified',
                    columnHidden:true
                }
            ]
        };

        //搜索属性配置，如果字段不配置，默认是input搜索
        // 搜索类型：
        //      1、输入框 type='input'， 默认
        //      2、时间 type='date'
        //      3、数字 type='number'
        //      4、下拉框 type='select'
        this.searchComponentData = [
            {
                name:'groupType',
                type: 'select',
                optionList:[
                    {
                        optionKey:'',
                        optionShow:''
                    },{
                        optionKey:'1',
                        optionShow:'相斥'
                    },{
                        optionKey:'2',
                        optionShow:'相似'
                    }
                ]
            },{
                name:'forceSeparate',
                type: 'select',
                optionList:[
                    {
                        optionKey:'',
                        optionShow:''
                    },{
                        optionKey:'1',
                        optionShow:'是'
                    },{
                        optionKey:'0',
                        optionShow:'否'
                    }
                ]
            },{
                name:'activeFlag',
                type: 'select',
                optionList:[
                    {
                        optionKey:'',
                        optionShow:''
                    },{
                        optionKey:'1',
                        optionShow:'启用'
                    },{
                        optionKey:'2',
                        optionShow:'禁用'
                    }
                ]
            },{
                name:'gmtCreate',
                type: 'date',
                maxName:'gmtCreateMax', //最大值， 选择小于时使用；选择范围时大值使用
                minName:'gmtCreateMin'  //最小值， 选择大于时使用；选择范围时小值使用
            },{
                name:'gmtModified',
                type: 'date',
                maxName:'gmtModifiedMax', 
                minName:'gmtModifiedMin'
            }
        ];

    }

// 行操作
    rowHandles = (strHandles, rowObj) => {
        if(strHandles === 'edit'){
            this.editHandle(rowObj);
        }else if(strHandles === 'delete'){
            this.deleteHandle(rowObj);
        }else if(strHandles === 'add'){
            this.addHandle();
        }
    }

    addHandle = () => {
        this.modalTitle = '新增';
        this.setState({
            addFlag:true,
            formData:{}
        });
        this.props.form.resetFields(); //初始化表单
        this.showModal();
    }

    editHandle = (rowObj) => {
        console.log('--editHandle----',rowObj);
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else{
            this.modalTitle = '修改';
            this.showModal();
            let id = rowObj[0].data.productCompatibleId;
            let options={
                url:'/base/data/productcompatible/r/'+id,
                type:'get',
                condition:{}
            };

            fetchData(this, options, (resData) => {
                if (resData.code == '1'){
                    this.setState({
                        formData:resData.data
                    });
                    this.showModal();
                }else {
                    message.error(resData.msg);
                }
            });
            
        }
    }

    deleteHandle = (rowObj) => {
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else{
            let id = rowObj[0].data.productCompatibleId;
            let options={
                url:'/base/data/productcompatible/d',
                condition:{productCompatibleId:id}
            };
            let _that = this;
            confirm({
                title: '您确定要删除吗？',
                onOk() {
                    fetchData(_that, options, (resData) => {
                        _that.setState({
                            resData: resData
                        });
                        if (resData.code == '0'){
                            alert(resData.msg);
                        }else {
                            _that.refreshHandle();
                        }
                    });
                },
                onCancel() {
                    console.log("取消了删除操作")
                },
            });
        }
    }

    showModal = () =>{
        this.setState({
            visible:true
        });
        
    }
    // 右上角x
    handleCancel = () => {
        this.resetFrom();
    }
    // 取消btn
    resetFrom = () => {
        this.props.form.resetFields(); //初始化表单
        // 注意清空selectList
        this.setState({
            visible:false
        })
    }

    //刷新列表
    refreshHandle = () => {
        this.refs.productCompatibleList.refreshHandle();
    }

    handleSubmit = (e) => {
        e.preventDefault();
        this.props.form.validateFieldsAndScroll((err, values) => {
                if (!err) {
                    this.setState({loading: true});
                    //发送ajax 控制请求数据规则，无值的设为空
                    console.log('表单提交数据-----------',values);
                    for (let item in values) {
                        if (!values[item]) {
                            values[item] = ''
                        }
                    }
                    console.log('Received values of form: ', values);
                    let url='';
                    let addFlag = this.state.addFlag;
                    console.log('addFlag====',addFlag);
                    if(addFlag){
                        url = '/base/data/productcompatible/c';
                    }else{
                        url = '/base/data/productcompatible/u';
                    }
  
                    const options = {
                        url: url,
                        type: 'post',
                        condition: values
                    };
                    fetchData(this, options, (res) => {
                        //回调函数
                        if (res.code == '1') {
                            message.success('操作成功');
                            this.refreshHandle();
                        }else{
                            message.error(res.msg);
                        }
                        setTimeout(() => {
                                this.setState({visible: false,loading: false})
                        }, 1000); //缓冲
                        
                    })
                }
            });
    }

    productCodeSearchClick = () =>{
        this.refs.productCodeModalTable.showModal();
    }

    getProductCodeTableData =(selectRowObj) => {
        this.props.form.setFieldsValue(
            {
                productCode:selectRowObj.productCode,
                productName:selectRowObj.productDescZh
            }
            );
    }

    render() {
        //当左侧隐藏时，列表宽度加上左侧的宽度，
        //1、当前this.props.isHideLeft 是从页面组件containers/Main/main.js传过来
        //2、this.props.isHideLeft 最顶级赋值是在页面 containers/Main/main.js
        let isHideLeft = this.props.isHideLeft;
        var modalTitle='';
        const colNumber = 12;

        const {getFieldDecorator} = this.props.form;
        const formItemLayout = {
            labelCol: {
                xs: {span: 24},
                sm: {span: 6}
            },
            wrapperCol: {
                xs: {span: 24},
                sm: {span: 14}
            }
        };
        const tailFormItemLayout = {
            wrapperCol: {
                xs: {span: 24,offset: 0},
                sm: {span: 24,offset: 10}
            }
        };

        return(
            <div>
                <ListBox
                    ref = 'productCompatibleList'
                    isHideLeft={isHideLeft}
                    title=" 产品相容-低"
                    tableColumnData = {this.tableColumn}
                    searchComponentData={this.searchComponentData}
                    url = '/base/data/productcompatible/list'
                    rowHandles = {this.rowHandles}
                    rowOperation = {this.rowOperation}
                    headerOperation = {this.headerOperation}
                    isRowSingle = "true"
                />
                <Modal
                    style={{top:'20px'}}
                    visible={this.state.visible}
                    title={this.modalTitle}
                    onCancel={this.handleCancel}
                    width='850px'
                    footer=''
                    maskClosable={false}
                >
                    <Form
                        onSubmit={this.handleSubmit}
                        style={{
                        backgroundColor: '#fff',
                        paddingTop: '20px',
                        paddingBottom: '20px'
                    }}>

                        <Row>
                            <Col span={0}>
                                <FormItem>
                                    {getFieldDecorator('productCompatibleId', {
                                        initialValue: this.state.formData.productCompatibleId,
                                        rules: [
                                            {}
                                        ]
                                    })(<Input type="hidden"/>)}
                                </FormItem>
                            </Col>
                            <Col span={colNumber}>
                                <FormItem {...formItemLayout} label="组编码" hasFeedback>
                                    {getFieldDecorator('groupCode', {
                                        initialValue: this.state.formData.groupCode,
                                        rules: [
                                            {
                                                required: true, message: '请输入组编码!'
                                            }
                                        ]
                                    })(<Input/>)}
                                </FormItem>
                            </Col>
                            <Col span={colNumber}>
                                <FormItem {...formItemLayout} label="组名称" hasFeedback>
                                    {getFieldDecorator('groupName', {
                                        initialValue:this.state.formData.groupName,
                                        rules: [
                                            {
                                                required: true, message: '请输入组名称!'
                                            }
                                        ]
                                    })(<Input />)}
                                </FormItem>
                            </Col>
                            <Col span={colNumber}>
                                <FormItem {...formItemLayout} label="产品编码" hasFeedback>
                                    <Row gutter={8}>
                                        <Col span={22}>
                                            {getFieldDecorator('productCode', {
                                                initialValue:this.state.formData.productCode,
                                                rules: [{
                                                         required: true, message: '请选择产品编码'
                                                    }]
                                            })(
                                                <Input disabled/>
                                            )}
                                        </Col>
                                        <Col span={2}>
                                            <Icon type="search" style={{cursor:'pointer'}} onClick={this.productCodeSearchClick} />
                                        </Col>
                                    </Row>
                                </FormItem>
                            </Col>
                            <Col span={colNumber}>
                                <FormItem {...formItemLayout} label="产品名称" hasFeedback>
                                    {getFieldDecorator('productName', {
                                        initialValue:this.state.formData.productName,
                                        rules: [
                                            {}
                                        ]
                                    })(<Input disabled/>)}
                                </FormItem>
                            </Col>
                            <Col span={colNumber}>
                                <FormItem
                                    {...formItemLayout}
                                    label="组类型"
                                >
                                    <Row gutter={8}>
                                        {getFieldDecorator('groupType', {
                                            initialValue:this.state.formData.groupType,
                                            rules: [{
                                                     required: true, message: '请选择组类型!'
                                                }]
                                        })(
                                            <Select placeholder="请选择" showSearch allowClear >
                                                {
                                                     this.state.groupTypeList.map((es,sIndex)=>{
                                                        return (
                                                            <Option key={sIndex} value={es.selectValue}>{es.SelectName}</Option>
                                                        )
                                                    })
                                                }
                                            </Select>
                                        )}
                                    </Row>
                                </FormItem>
                            </Col>
                            <Col span={colNumber}>
                                <FormItem
                                    {...formItemLayout}
                                    label="强制分离"
                                >
                                    <Row gutter={8}>
                                        {getFieldDecorator('forceSeparate', {
                                            initialValue:this.state.formData.forceSeparate,
                                            rules: [{
                                                     //required: true, message: '请选择组类型!'
                                                }]
                                        })(
                                            <Select placeholder="请选择" showSearch allowClear >
                                                {
                                                     this.state.forceSeparateList.map((es,sIndex)=>{
                                                        return (
                                                            <Option key={sIndex} value={es.selectValue}>{es.SelectName}</Option>
                                                        )
                                                    })
                                                }
                                            </Select>
                                        )}
                                    </Row>
                                </FormItem>
                            </Col>
                            <Col span={colNumber}>
                                <FormItem
                                    {...formItemLayout}
                                    label="激活标志"
                                >
                                    <Row gutter={8}>
                                        {getFieldDecorator('activeFlag', {
                                            initialValue:this.state.formData.activeFlag,
                                            rules: [{
                                                     required: true, message: '请选择激活标志!'
                                                }]
                                        })(
                                            <Select placeholder="请选择" showSearch allowClear >
                                                {
                                                     this.state.activeFlagList.map((es,sIndex)=>{
                                                        return (
                                                            <Option key={sIndex} value={es.selectValue}>{es.SelectName}</Option>
                                                        )
                                                    })
                                                }
                                            </Select>
                                        )}
                                    </Row>
                                </FormItem>
                            </Col>

                            <Col span={24}>
                                <FormItem {...tailFormItemLayout}>
                                    <Button 
                                        type="primary" 
                                        htmlType="submit" 
                                        size="large"
                                    >
                                        {this.state.loading?'提交中...':'确定'}
                                    </Button>
                                    <Button
                                        type="default"
                                        style={{marginLeft: '50px'}}
                                        size="large"
                                        onClick={this.resetFrom}
                                    >
                                        取消
                                    </Button>
                                </FormItem>
                            </Col>

                        </Row>

                    </Form>
                </Modal>
                <ProductModalTable getTableData={this.getProductCodeTableData} ref='productCodeModalTable' />
            </div>
        );
    }

}
export default Form.create()(CompatibleManage); 